package org.easydarwin.easyplayer.zhi;

import android.hardware.Camera;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.github.czy1121.view.RoundButton;

import org.easydarwin.easyplayer.R;
import org.easydarwin.easyplayer.bean.ChaXun;
import org.easydarwin.easyplayer.http.API;
import org.easydarwin.easyplayer.http.Okhttp;
import org.easydarwin.easyplayer.utils.JSONUtils;
import org.easydarwin.easyplayer.utils.Util;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * 设置IP
 */
public class SetIp_Fen_Activity extends AppCompatActivity {


    @BindView(R.id.img_back)
    RelativeLayout imgBack;
    @BindView(R.id.textView4)
    TextView textView4;
    @BindView(R.id.qs)
    RoundButton qs;
    @BindView(R.id.sjrxx)
    TextView sjrxx;
    @BindView(R.id.tv_fenbianlv)
    TextView tv_fenbianlv;

    @BindView(R.id.spinner2)
    Spinner spnResolution;
    List<String> listResolution = new ArrayList<String>();
    int width = 640, height = 480;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_fenbian_);
        ButterKnife.bind(this);
        getWidth_Height();
        Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
        StringBuilder stringBuilder = new StringBuilder();

        Camera.getCameraInfo(0, cameraInfo);
        Camera camera = Camera.open(0);
        Camera.Parameters params = camera.getParameters();
        List<Camera.Size> previewSIzes = params.getSupportedVideoSizes();
        for (Camera.Size str : previewSIzes) {
            stringBuilder.append(str.width + "x" + str.height).append(";");
        }
        Log.e("111", "数据：" + stringBuilder);
        Util.saveSupportResolution(SetIp_Fen_Activity.this, stringBuilder.toString());


        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                listResolution = Util.getSupportResolution(getApplicationContext());
                boolean supportdefault = listResolution.contains(String.format("%dx%d", width, height));
                if (!supportdefault) {
                    String r = listResolution.get(0);
                    String[] splitR = r.split("x");
                    width = Integer.parseInt(splitR[0]);
                    height = Integer.parseInt(splitR[1]);
                }
                initSpninner();
            }
        });

    }

    public static String cameraSizeToSting(Iterable<Camera.Size> sizes) {
        StringBuilder s = new StringBuilder();
        for (Camera.Size size : sizes) {
            if (s.length() != 0)
                s.append(",");
            s.append(size.width).append('x').append(size.height);
        }
        return s.toString();
    }

    private void initSpninner() {
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.spn_item, listResolution);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spnResolution.setAdapter(adapter);
        int position = listResolution.indexOf(String.format("%dx%d", width, height));
        spnResolution.setSelection(position, false);
        spnResolution.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                String r = listResolution.get(position);
                String[] splitR = r.split("x");

                int wh = Integer.parseInt(splitR[0]);
                int ht = Integer.parseInt(splitR[1]);
                if (width != wh || height != ht) {
                    width = wh;
                    height = ht;

                }

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    public void setWidth_Height(final String width,final String height) {
        API.setWidthhi(width, height, new Okhttp.Objectcallback() {
            @Override
            public void onsuccess(String st) {
                Log.e("111" ,"xx"+st);
                tv_fenbianlv.setText("当前分辨率:"+height+"x"+width );
                Toast.makeText(SetIp_Fen_Activity.this, "设置分辨率成功!", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFalia(int code, String errst) {
                Log.e("111" ,"xx"+errst);
                Toast.makeText(SetIp_Fen_Activity.this, errst, Toast.LENGTH_SHORT).show();
            }
        });

    }

    @OnClick({R.id.img_back, R.id.qs})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.qs:
                setWidth_Height(String.valueOf(width), String.valueOf(height));
                break;
        }
    }

    public void getWidth_Height( ) {
        API.getWidthhi(  new Okhttp.Objectcallback() {
            @Override
            public void onsuccess(String st) {
                Log.e("111" ,"xx"+st);
                ChaXun chaXun = JSONUtils.parseJSON(st, ChaXun.class);
                String height = chaXun.getData().getHeight();
                String width = chaXun.getData().getWidth();
                tv_fenbianlv.setText("当前分辨率:"+height+"x"+width );
            }

            @Override
            public void onFalia(int code, String errst) {
                Log.e("111" ,"xx"+errst);
            }
        });

    }


}
